package com.houtai.eduservice.controller;


import com.houtai.commonutils.Result;
import com.houtai.eduservice.config.OperationLogDesc;
import com.houtai.eduservice.entity.EduChapter;
import com.houtai.eduservice.entity.chapter.ChapterVo;
import com.houtai.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-11-20
 */
@Api(description = "课程章节")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;
    //课程章节，通过id查询
    @GetMapping("getChapterVideo/{courseId}")
    @ApiOperation(value = "获取课程章节")
    @OperationLogDesc(module = "课程模块",description = "查看课程章节")
    public Result getChapterVideo(@PathVariable Integer courseId){
        List<ChapterVo> list= eduChapterService.getChapterVideoById(courseId);
        return Result.ok().data("allChapterVideo",list);
    }
    //添加章节
    @PostMapping("addChapter")
    @ApiOperation(value = "添加章节")
    @OperationLogDesc(module = "课程模块",description = "添加章节")
    public Result addChapter(@RequestBody @Valid EduChapter eduChapter){
         eduChapterService.save(eduChapter);
        return Result.ok();
    }
    //根据id查询
    @GetMapping("getChapterInfo/{chapterId}")
    @OperationLogDesc(module = "课程模块",description = "获取章节信息")
    @ApiOperation(value = "获取章节信息")
    public Result getChapterInfo(@PathVariable Integer chapterId){
        EduChapter eduChapter=eduChapterService.getById(chapterId);
        return Result.ok().data("chapter",eduChapter);
    }
    //修改章节
    @PostMapping("updateChapter")
    @OperationLogDesc(module = "课程模块",description = "修改章节信息")
    @ApiOperation(value = "修改章节")
    public Result updateChapter(@RequestBody @Valid EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return Result.ok();
    }
    //删除章节
    @DeleteMapping("deleteChapter/{chapterId}")
    @OperationLogDesc(module = "课程模块",description = "删除章节")
    @ApiOperation(value = "删除章节")
    public Result deleteChapter(@PathVariable Integer chapterId){
        boolean flag= eduChapterService.deleteChapter(chapterId);
        if(flag){
            return Result.ok();
        }else {
            return Result.error();
        }

    }


}

